package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import de.dnb.basics.applicationComponents.DbProperties;
import de.dnb.basics.applicationComponents.DbUtils;

public class FirstSqlAccess {

	private Connection con;
	private Statement stmt;
	private ResultSet rs;

	public FirstSqlAccess() throws SQLException {
		DbProperties properties = new DbProperties("D:/db/db.properties");
		String url = properties.getUrl()
				+ ";ifexists=false;create=false;readonly=true";
		con = DriverManager.getConnection(url, properties.getUserName(),
				properties.getPassword());
		stmt = con.createStatement();
		System.err.println("constructor");
	}

	public void doQuery() throws SQLException {
		rs = stmt.executeQuery("SELECT * FROM sw WHERE name LIKE 'phys%'");
		while (rs.next()) {
			System.out.println(rs.getString("id") + " / "
					+ rs.getString("name"));
		}
	}

	@Override
	protected void finalize() throws Throwable {
		System.out.println("finalizer");
		close();
	}

	/**
	 * @throws SQLException
	 */
	protected void close() throws SQLException {
		DbUtils.safeCloseResultSet(rs);
		DbUtils.safeCloseStatement(stmt);
		DbUtils.safeCloseConnection(con);
		System.err.println("all closed");
	}

	public static void main(String[] args) {
		FirstSqlAccess accessor;
		try {
			accessor = new FirstSqlAccess();
			accessor.doQuery();
			accessor.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}